package com.zxl.openapi.listener;


import com.zxl.openapi.feign.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;

import static com.zxl.openapi.constans.SystemParameter.REDIS_APIPRE;
import static com.zxl.openapi.constans.SystemParameter.allApiInfoMap;

@Component
public class MyRabbitMQListener {

    Logger logger = LoggerFactory.getLogger(MyRabbitMQListener.class);

    @Autowired
    private RedisService redisService;

    //声明接受这个的
    @StreamListener("1907openapi-z")
    public void onMessage(String message){

        System.out.println("收到了消息:" + message);
//
        //在这里加载数据 ->链接redis 查询数据
        Set<String> keys = redisService.keys(REDIS_APIPRE + "*");

        allApiInfoMap.clear();

        for (String key : keys) {

            Map<Object, Object> result = redisService.hgetAll(key);

            allApiInfoMap.put(key,result);

        }

        logger.info("更新api信息，得到的结果是：{}",allApiInfoMap);

    }

}
